package com.example.module_hp_zao_yin_media.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.widget.MediaController;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.example.module_hp_zao_yin_media.base.BaseMvvmActivity;
import com.example.module_hp_zao_yin_media.base.BaseViewModel;
import com.example.module_hp_zao_yin_media.R;
import com.example.module_hp_zao_yin_media.databinding.ActivityHpZaoYinVideoInfoBinding;
import com.example.module_hp_zao_yin_media.util.DownBitmap;
import com.example.module_hp_zao_yin_media.util.Utils;
import com.fwlst.lib_base.utils.AdUtils;
import com.fwlst.lib_base.utils.JtXXPermissions;
import com.hjq.permissions.Permission;
import com.hjq.toast.Toaster;
import com.otaliastudios.cameraview.VideoResult;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class HpZaoYinVideoInfoActivity extends BaseMvvmActivity<ActivityHpZaoYinVideoInfoBinding, BaseViewModel> {
    private Uri uri;
    public VideoResult result;
    @Override
    protected int initLayout() {
        return R.layout.activity_hp_zao_yin_video_info;
    }

    @Override
    protected int initBR() {
        return 0;
    }

    @Override
    protected void initView() {
        result = Utils.result;
        if(result==null){
            finish();
            return;
        }
        //视频播放
        uri = Uri.fromFile(result.getFile());
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(this,binding.bannerContainer);
        //点击事件-返回
        binding.hpZaoYinPhotoBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //点击事件-保存
        binding.hpZaoYinPhotoBt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPermission();
            }
        });

        binding.hpZaoYinPhotoVv.setVideoURI(uri);
        MediaController mediaController = new MediaController(mContext);
        mediaController.setAnchorView(binding.hpZaoYinPhotoVv);
        binding.hpZaoYinPhotoVv.setMediaController(mediaController);
        binding.hpZaoYinPhotoVv.start();
    }
    private void getPermission(){
        JtXXPermissions.INSTANCE.requestGallery(this, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                copyFileByUri();
                return null;
            }
        });
    }

    //保存视频到本地
    public void copyFileByUri() {
        //存入Download文件夹
        String savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        String filename = String.valueOf(Calendar.getInstance().getTimeInMillis());

        File saveFile = new File(savePath, filename+".mp4");

        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        try {
            inStream = new FileInputStream(new File(uri.getPath()));
            outStream = new FileOutputStream(saveFile);
            FileChannel inChannel = inStream.getChannel();
            FileChannel outChannel = outStream.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
            //弹窗提示
            DownBitmap.succeedDialog(mContext,Environment.DIRECTORY_DOWNLOADS);
            //把图片保存后声明这个广播事件通知系统相册有新图片到来
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri newUri = Uri.fromFile(new File(savePath+"/"+filename+".mp4"));
            intent.setData(newUri);
            mContext.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }

                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if(!binding.hpZaoYinPhotoVv.isPlaying()){
            binding.hpZaoYinPhotoVv.start();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}